﻿using System;
using UnityEngine.UI;


namespace HEFramework
{
    public static class StatisticUISystem
    {
        [EntitySystem]
        public class StatisticUIAwakeSystem : AwakeSystem<StatisticUI>
        {
            protected override void Awake(StatisticUI _self)
            {
                _self.Open();
            }
        }


        [EntitySystem]
        public class StatisticUIUpdateSystem : UpdateSystem<StatisticUI>
        {
            protected override void Update(StatisticUI _self)
            {
                _self.Update();
            }
        }


        [EntitySystem]
        public class StatisticUIDestroySystem : DestroySystem<StatisticUI>
        {
            protected override void Destroy(StatisticUI _self)
            {
                _self.Close();
            }
        }


        public static void Open(this StatisticUI _self)
        {
            var uc = _self.GetSceneChild<UIComponent>();
            if (uc == null)
            {
                return;
            }

            uc.PushUIAsync(eUIFlag.Statistic, _ =>
            {
                _self.UIWindowBehaviour = _;
                _self.Bind();
                _self.Index = 0;
                _self.ClosePanel();
                _self.Update(true);
            });
        }


        public static void Bind(this StatisticUI _self)
        {
            _self.UIWindowBehaviour.GetButton("Button_Open").RegisterDownEvent(_self.ShowPanel);
            _self.UIWindowBehaviour.GetButton("Button_Close").RegisterDownEvent(_self.ClosePanel);
        }

        private static void Update(this StatisticUI _self, bool _isNew = false)
        {
            if (_self.UIWindowBehaviour == null || _self.UIWindowBehaviour.State == 0)
            {
                return;
            }

            var sc = _self.GetSceneChild<Statistic>();
            if (sc == null)
            {
                return;
            }

            for (int i = 0; i < 10; i++)
            {
                if (_self.Index < sc.StatisticDatas.Count)
                {
                    var data = sc.StatisticDatas[_self.Index];
                    _self.Index++;

                    _self.StringBuilder.Clear();
                    var text = _self.UIWindowBehaviour.GetTMPText("TMPText_Message").Text;

                    if (text.text.Length > 10000)
                    {
                        text.text = "";
                    }

                    switch (data.Type)
                    {
                        case Statistic.eType.Normal:
                            _self.StringBuilder.AppendLine(text.text);
                            _self.StringBuilder.Append(data.Message);
                            break;
                        case Statistic.eType.Battle:
                        {
                            _self.StringBuilder.AppendLine(text.text);
                            _self.StringBuilder.Append(_self.GetAttributeName(data.A_ID));
                            _self.StringBuilder.Append(" 对 ");
                            _self.StringBuilder.Append(_self.GetAttributeName(data.B_ID));

                            if (data.HP > 0)
                            {
                                _self.StringBuilder.Append(" 恢复生命 ");
                                _self.StringBuilder.Append("<color=#00FF00>" + Math.Abs((float)data.HP) + "</color>");
                            }
                            else if (data.HP < 0)
                            {
                                _self.StringBuilder.Append(" 造成伤害 ");
                                switch (data.PowerType)
                                {
                                    case ePowerType.Physics:
                                        _self.StringBuilder.Append("<color=#FF7F24>" + Math.Abs((float)data.HP) + "</color>");
                                        break;
                                    case ePowerType.Magic:
                                        _self.StringBuilder.Append("<color=#0000FF>" + Math.Abs((float)data.HP) + "</color>");
                                        break;
                                }
                            }


                            if (data.MP > 0)
                            {
                                _self.StringBuilder.Append(" 恢复魔法 ");
                                _self.StringBuilder.Append(Math.Abs((float)data.MP));
                            }
                            else if (data.MP < 0)
                            {
                                _self.StringBuilder.Append(" 减少魔法 ");
                                _self.StringBuilder.Append(Math.Abs((float)data.MP));
                            }
                        }
                            break;
                    }

                    text.text = _self.StringBuilder.ToString();
                }
            }
        }

        public static void Close(this StatisticUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.Hide();
            _self.UIWindowBehaviour = null;
        }


        private static void ShowPanel(this StatisticUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetTransform("Transform_Panel").gameObject.SetActive(true);
            _self.UIWindowBehaviour.GetButton("Button_Open").GetGameObject().SetActive(false);
            _self.UIWindowBehaviour.GetTransform("Transform_View").GetComponent<ScrollRect>().verticalScrollbar.value = 0;
        }

        private static void ClosePanel(this StatisticUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetTransform("Transform_Panel").gameObject.SetActive(false);
            _self.UIWindowBehaviour.GetButton("Button_Open").GetGameObject().SetActive(true);
        }

        private static string GetAttributeName(this StatisticUI _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return "";
            }

            var table = dc.LoadDataTable<DRAttribute>();
            var data = table.GetData(_id);
            return data?.Name;
        }

        private static string GetSkillName(this StatisticUI _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return "";
            }

            var table = dc.LoadDataTable<DRSkill>();
            var data = table.GetData(_id);
            return data?.Name;
        }
    }
}